package com.project.servlet;

import com.project.ServletObj;
import jdk.nashorn.internal.ir.RuntimeNode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@ServletObj(url="/addUser")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //设置请求信息的编码
        req.setCharacterEncoding("utf-8");
        //设置响应信息的MIME类型和编码集
        resp.setContentType("text/html;charset=utf-8");

        String info = "";
        //根据表单名，得到表单值
        String userName = req.getParameter("userName");
        String birthdayStr =  req.getParameter("birthday");
        info += "姓名:"+userName+"<br>生日："+birthdayStr+"<br>";

        String edu = req.getParameter("edu");
        String sex = req.getParameter("sex");

        info += "学历："+edu+"<br>性别："+sex+"<br>";
        //客户端提交多个同名的键值对，利用req.getParameterValues得到数组
        String[] likeArray = req.getParameterValues("like");
        info += "爱好："+ Arrays.toString(likeArray)+"<br>";
        String like = req.getParameter("like");
        info += "爱好------："+like;

        resp.getWriter().print(info);
    }
}
